package com.fzhucc.modules.businessCard.service.impl;

import com.fzhucc.exception.BadRequestException;
import com.fzhucc.modules.businessCard.domain.BusinessCardConfig;
import com.fzhucc.modules.businessCard.domain.BusinessCardPre;
import com.fzhucc.modules.businessCard.domain.dto.BusinessCardConfigDto;
import com.fzhucc.modules.businessCard.domain.dto.BusinessCardConfigQueryCriteria;
import com.fzhucc.modules.businessCard.domain.dto.BusinessCardPreDto;
import com.fzhucc.modules.businessCard.domain.dto.BusinessCardPreQueryCriteria;
import com.fzhucc.modules.businessCard.repository.BusinessCardConfigRepository;
import com.fzhucc.modules.businessCard.repository.BusinessCardPreRepository;
import com.fzhucc.modules.businessCard.service.BusinessCardConfigService;
import com.fzhucc.modules.businessCard.service.BusinessCardPreService;
import com.fzhucc.modules.businessCard.service.mapstruct.BusinessCardConfigMapper;
import com.fzhucc.modules.businessCard.service.mapstruct.BusinessCardPreMapper;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import com.fzhucc.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yyg
 * @description 服务实现
 * @date 2022-12-21
 **/
@Service
@RequiredArgsConstructor
public class BusinessCardConfigServiceImpl implements BusinessCardConfigService {

    private final BusinessCardConfigRepository businessCardConfigRepository;
    private final BusinessCardConfigMapper businessCardConfigMapper;

    @Override
    public Map<String, Object> queryAll(BusinessCardConfigQueryCriteria criteria, Pageable pageable) {
        Page<BusinessCardConfig> page = businessCardConfigRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(businessCardConfigMapper::toDto));
    }

    @Override
    public List<BusinessCardConfigDto> queryAll(BusinessCardConfigQueryCriteria criteria) {
        return businessCardConfigMapper.toDto(businessCardConfigRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    public BusinessCardConfigDto findById(Integer id) {
        BusinessCardConfig businessCardConfig = businessCardConfigRepository.findById(id).orElseGet(BusinessCardConfig::new);
        ValidationUtil.isNull(businessCardConfig.getId(), "BusinessCardConfig", "id", id);
        return businessCardConfigMapper.toDto(businessCardConfig);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BusinessCardConfigDto create(BusinessCardConfig resources) {
        return businessCardConfigMapper.toDto(businessCardConfigRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(BusinessCardConfig resources) {
        BusinessCardConfig businessCardConfig = businessCardConfigRepository.findById(resources.getId()).orElseGet(BusinessCardConfig::new);
        ValidationUtil.isNull(businessCardConfig.getId(), "BusinessCardConfig", "id", resources.getId());
        businessCardConfig.copy(resources);
        businessCardConfigRepository.save(businessCardConfig);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            businessCardConfigRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<BusinessCardConfigDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (BusinessCardConfigDto businessCardConfig : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("名片icon label名称", businessCardConfig.getIconLabel());
            map.put("平台简介", businessCardConfig.getIntroduction());
            map.put("视频简介地址", businessCardConfig.getVideoIntroductionUrl());
            map.put("名片icon地址", businessCardConfig.getIconUrl());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}